<?php 
session_start();
/*
error_reporting(E_ALL);
ini_set("display_errors", 1);*/

require_once '../utility.php';
require_once '../../sys/config.php';
require_once PATH_PROVIDER."netfonds.php";
require_once PATH_DB_MAIN."TB_stockData.php";
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<script type="text/javascript">
function redirectSelfPage(){
	window.location = "import.php?" + new Date().getTime();
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Import test</title>
</head>
<?php 

if(isset($_SESSION["enc_servProv"])){
	$servProv = unserialize($_SESSION["enc_servProv"]);
	$stockName = unserialize($_SESSION["enc_stockName"]);
	$timestamp = unserialize($_SESSION["enc_timestamp"]);
}
else{
	$servProv = new $_POST["provider"]();
	$stockName = $_POST["stockName"];
	$strTime = str_replace('/', '-', $_POST["timestamp"]) . " 00:00:00 ".TIMEZONE_CODE;
	$timestamp = strtotime($strTime);
	
	$_SESSION["enc_servProv"] = serialize($servProv);
	$_SESSION["enc_stockName"] = serialize($stockName);
	$_SESSION["enc_timestamp"] = serialize($timestamp);
}

$max_exec_time = ini_get('max_execution_time')*1000;
if($max_exec_time <= 0)
	$max_exec_time = 60*1000;	//1 minuto
$max_exec_time -= 10000;
$exp = new TimerLoad($max_exec_time);

if($fileLineCnt > 0 && $max_exec_time > 2000){
	$delayTime = $max_exec_time - 1000;
	$nextStepString = "onLoad=\"setTimeout('redirectSelfPage()', $delayTime)\"";
}

echo "<body $nextStepString >";
echo "<table><tr><td>nextStepString</td><td>$nextStepString</td></tr>";

require_once '../../sys/config.php';
require_once PATH_PROVIDER."serviceprovider.php";
require_once PATH_PROVIDER."netfonds.php";
require_once PATH_DB_MAIN."TB_stockData.php";

$tbl = new TBL_StockData($stockName, $servProv->GetStockDataType(), $servProv->HasTickData());
$lastDataTs = $tbl->LastDataTimestamp();

$fileLineCnt = $servProv->BeginStockRead($stockName, $timestamp, $lastDataTs[0], $lastDataTs[1]);

echo "<tr><td>max_exec_time</td><td>$max_exec_time</td></tr></table>";

$recCnt = 0;
if($fileLineCnt > 0){

	while((($stockData1 = $servProv->GetNextData()) != FALSE) && ($exp->IsExpired() == FALSE)){
		$tbl->SaveData($stockData1);
		$fileLineCnt--;
	}
}
else{
	echo "FINITO TUTTO!!!!";
}
$_SESSION["enc_fileLineCnt"] = serialize($fileLineCnt);

?>
</body>
</html>